<?php
//by willson 105328468@qq.com 2014/10/11
//index page interface

class commission_model extends base_model {

	public function __construct() {
		parent::__construct();
	}

	//写入佣金表
	public function add($member_id, $parent_id=0, $type=1) {
		//订单佣金
		$status = 0;
		$memo = '';
		$commission = 0;
		if ($type==1) {
			$memo = '订单佣金收入';
			$sql = "SELECT total_price FROM {$this->tbp}order WHERE id='{$parent_id}'";
			$order_price = floatval($this->db->get_var($sql));
			$commission = $this->calculate($order_price, 1);
		} else if ($type==2) {
			$status = 1;
			$memo = '充值收入';
			$sql = "SELECT recharged_money FROM {$this->tbp}member_recharge WHERE id='{$parent_id}'";
			$recharged_money = floatval($this->db->get_var($sql));
			$commission = $this->calculate($recharged_money, 2);
		} else if ($type==3) {
			$status = 1;
			$memo = '邀请用户注册收入';
			$commission = $this->calculate(0, 3);
		}
		$sql = "INSERT INTO {$this->tbp}member_commission(member_id, type, parent_id, commission, memo, status, add_time)
			VALUES('{$member_id}', '{$type}', '{$parent_id}', '{$commission}', '{$memo}', '{$status}', '".time()."')";
		$this->db->query($sql);
	}

	//结算佣金
	public function jiesuan($parent_id, $type=1){
		$commission = 0;
		$sql = "SELECT * FROM {$this->tbp}member_commission WHERE type='{$type}' AND parent_id='{$parent_id}'";
		$row = $this->db->get_row($sql);
		if ($row) {
			if ($row->status!=2) {
				$commission = $row->commission;
				//更新状态
				$sql = "UPDATE {$this->tbp}member_commission SET status='2' WHERE id='{$row->id}'";
				$this->db->query($sql);
				//更新用户的佣金
				$sql = "UPDATE {$this->tbp}member SET commission=commission+{$row->commission} WHERE id='{$row->member_id}'";
				$this->db->query($sql);
			}
		}
		return $commission;
	}

	//取消佣金
	public function cancel($parent_id, $type=1){
		$commission = 0;
		$sql = "SELECT * FROM {$this->tbp}member_commission WHERE type='{$type}' AND parent_id='{$parent_id}'";
		$row = $this->db->get_row($sql);
		if ($row) {
			$commission = $row->commission;
			//原本是已经结算的状态，需要减去
			if ($row->status==1) {
				$sql = "SELECT commission FROM {$this->tbp}member WHERE id='{$row->member_id}'";
				$member_commission = $this->db->get_var($sql);
				$new_commission = $member_commission - $commission;
				//允许负数的佣金出现
				//if ($new_commission < 0) $new_commission = 0;
				//更新用户的佣金
				$sql = "UPDATE {$this->tbp}member SET commission={$new_commission} WHERE id='{$row->member_id}'";
				$this->db->query($sql);
				//更新状态
				$sql = "UPDATE {$this->tbp}member_commission SET status='-1' WHERE id='{$row->id}'";
				$this->db->query($sql);
			} else {
				//更新状态
				$sql = "UPDATE {$this->tbp}member_commission SET status='-1' WHERE id='{$row->id}'";
				$this->db->query($sql);
			}
		}
		return $commission;
	}

	//计算佣金 type=1：订单佣金
	public function calculate($money, $type=1) {
		$commission = 0;
		switch ($type) {
			case 1:
				$percent = floatval($this->configs['invite_order_commission']);
				if ($percent) $commission = number_format($money * $percent, 2);
				break;
			case 2:
				$commission = $money;
				break;
			case 3:
				//注册的佣金是固定的金额，不是百分比
				$commission = floatval($this->configs['invite_register_commission']);
				//if ($percent) $commission = number_format($money * $percent / 100, 2);
				break;
		}
		return $commission;
	}

	//状态
	public function status_name($status) {
		$str = $status;
		switch ($status) {
			case -1:$str = '退款';break;
			case 0:$str = '冻结';break;
			case 1:$str = '正常(已到会员钱包)';break;
		}
		return $str;
	}
	
	//转到可用佣金
	public function turn_commission($order_id) {
		//不应该由订单获取受益人id, 下单时已在member_commission表生成对应记录
		//$sql = "SELECT o.*, m.parent_id as parent_member_id FROM {$this->tbp}order o LEFT JOIN {$this->tbp}member m ON o.member_id=m.id WHERE o.id='{$order_id}'";
		$sql = "SELECT * FROM {$this->tbp}order WHERE id='{$order_id}'";
		$order = $this->db->get_row($sql);
		if ($order->status>2) {
			//查出佣金
			$sql = "SELECT id, commission, member_id FROM {$this->tbp}member_commission 
				WHERE status=0 AND type=1 AND parent_id='{$order_id}'";
			$commission = $this->db->get_row($sql);
			if ($commission) {
				//佣金转到会员表
				$sql = "UPDATE {$this->tbp}member SET commission = commission + {$commission->commission} WHERE id='{$commission->member_id}'";
				$rs = $this->db->query($sql);
				if ($rs) {
					//更新佣金状态
					$sql = "UPDATE {$this->tbp}member_commission SET status=1 WHERE id='{$commission->id}'";
					$this->db->query($sql);
				}
			}
		}
	}
}
